package com.godyao.framework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.godyao.framework.beans.factory.config.BeanDefinition;
import com.godyao.framework.stereotype.Component;
import com.godyao.framework.stereotype.Repository;
import com.godyao.framework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author godyao
 */
public class ClassPathScanningCandidateComponentProvider {

    /**
     * 根据提供的包路径，扫描出在该路径下的所有被@Component,@Service,@Repository注解的类
     * @param basePackages
     * @return
     */
    public Set<BeanDefinition> findCandidateComponents(String basePackages) {
        LinkedHashSet<BeanDefinition> beanDefinitionSet = new LinkedHashSet<>();
        List<Class<? extends Annotation>> scanClasses = Arrays.asList(Component.class, Service.class, Repository.class);
        for (Class<? extends Annotation> scanClass : scanClasses) {
            Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackages, scanClass);
            for (Class<?> aClass : classes) {
                beanDefinitionSet.add(new BeanDefinition(aClass));
            }


        }
        return beanDefinitionSet;
    }
}
